package com.javaee.controller.admin;

import com.javaee.BeforeBaseController;
import com.javaee.entity.Doctor;
import com.javaee.entity.Plan;

import com.javaee.service.admin.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/plans")
public class PlanController extends BeforeBaseController {
    @Autowired
    private PlanService planService;

    @RequestMapping(method = RequestMethod.GET)
    public String listPlan(Model model, Integer currentPage,String keyword) {
        if(keyword == null){
            keyword = "";
        }
        if(currentPage == null)
        {
            currentPage = 1;
        }
        int totalCount = planService.selectCount();
        System.out.println("totalCount = " + totalCount);
        int pageSize = 5;
        int totalPage = (int)Math.ceil(totalCount*1.0/pageSize);
        List<Plan> plans = planService.findAllWithDoctor(keyword,(currentPage-1)*pageSize,pageSize);
        model.addAttribute("plans", plans);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        return "admin/planslist";
    }

    @RequestMapping("/new")
    public String newPlanForm(Model model) {
        List<Doctor> doctors=planService.finddoctorbyName();
        model.addAttribute("doctorList",doctors);
        model.addAttribute("plan", new Plan());
        model.addAttribute("doctors", new Doctor());
        return "admin/plannew";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String savePlan(Plan plan,Model model) {
        List<Plan>  planList= planService.findAll();
        List<Doctor> doctors=planService.finddoctorbyName();
        model.addAttribute("doctorList",doctors);
        for (Plan existingPlan:planList){
            if (existingPlan.getDoctorId().equals(plan.getDoctorId())&&
                    existingPlan.getWeekday().equals(plan.getWeekday())) {
                model.addAttribute("msg", "这个医生这天排班已经存在！");
                return "admin/plannew";
            }
        }
        planService.save(plan);
        return "redirect:/plans";
    }
    @RequestMapping("/{id}/edit")
    public String editPlanForm(@PathVariable Integer id, Model model) {
        Plan plan = planService.findById(id);
        List<Doctor> doctors=planService.finddoctorbyName();
        model.addAttribute("doctorList",doctors);
        model.addAttribute("plan", plan);
        return "admin/planedit";
    }

    @RequestMapping("/{id}")
    public String updatePlan(@PathVariable Integer id, @ModelAttribute("plan") Plan plan) {
        plan.setId(id);
        planService.update(plan);
        return "redirect:/plans";
    }

    @RequestMapping("/{id}/delete")
    public String deletePlan(@PathVariable Integer id) {
        planService.deleteById(id);
        return "redirect:/plans";
    }

    @RequestMapping("/search")
    public String searchPlansByName(@RequestParam String departmentName, Model model) {
        if(departmentName == null)
        {
            return "redirect:/plans";
        }
        else {
            List<Plan> plans = planService.findByDepartmentName(departmentName);
            model.addAttribute("plans", plans);
            return "admin/planslist";
        }
    }

}
